import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:inmotion_pro/pages/mine/setting/account_security/verify_code/verify_code_param.dart';
import 'package:inmotion_pro/service/global_service.dart';
import 'package:lx_widgets_library/lx_widgets_library.dart';

import '../../../../../l10n/local.dart';
import '../../../../../routes/app_routes.dart';
import '../../../../../utils/string_util.dart';
import '../../../../mixin/account_security_mixin.dart';
import '../../../../models/user_model.dart';
import 'controller.dart';

class VerifyAccountView extends BaseGetView<VerifyAccountController>
    with AccountSecurityMixin {
  VerifyAccountView({super.key});

  @override
  String get title => local.verify_account;

  @override
  Widget buildBody(BuildContext context) {
    final phone = GlobalService.to.cacheUserInfo.value.phone;
    final email = GlobalService.to.cacheUserInfo.value.email;
    debugPrint('VerifyAccountView buildBody phone: $phone, email: $email');
    return DismissKeyboardScaffold(
      resizeToAvoidBottomInset: false, // 防止键盘弹出时页面内容被顶起
      child: Container(
        width: double.infinity,
        height: double.infinity,
        color: Colors.black,
        child: SingleChildScrollView(
          // 添加滚动
          physics: const ClampingScrollPhysics(), // 防止滚动回弹
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Padding(
                padding: EdgeInsets.only(
                  left: CONTENT_MARGIN,
                  right: CONTENT_MARGIN,
                  top: CONTENT_MARGIN * 2,
                  bottom: CONTENT_MARGIN * 2,
                ),
                child: LxText(
                  local.verify_account_desc,
                  color: Colors.white.withOpacity(0.3),
                  fontWeightEnum: LxFontWeightEnum.bodySmall,
                ),
              ),
              Padding(
                padding: EdgeInsets.only(
                    left: CONTENT_MARGIN, right: CONTENT_MARGIN),
                child: Column(
                  children: [
                    Obx(
                      () => Visibility(
                        visible: controller.state.existEmailVerification.value,
                        child: _buildVerifyButton(
                            local.verify_account_by(
                                LxStringUtil.obscureEmail(email)), () {
                          accountSecurityRequestVerificationCode(
                            account: email,
                            platform: CodePlatform.email,
                            codeType: CodeType.security,
                          ).then((success) {
                            if (success) {
                              Get.toNamed(
                                AppRoutes.VERIFY_CODE,
                                arguments: VerifyCodeParam(
                                  account: email,
                                  platform: CodePlatform.email,
                                  codeType: CodeType.security,
                                  securityType: controller.securityType,
                                ),
                              );
                            } else {
                              Loading.showError(local.verify_code_send_failed);
                            }
                          });
                        }),
                      ),
                    ),
                    Obx(
                      () => Visibility(
                        visible: controller.state.existPhoneVerification.value,
                        child: _buildVerifyButton(
                            local.verify_account_by(
                                LxStringUtil.obscurePhone(phone)), () {
                          accountSecurityRequestVerificationCode(
                            account: phone,
                            platform: CodePlatform.phone,
                            codeType: CodeType.security,
                          ).then((success) {
                            if (success) {
                              Get.toNamed(
                                AppRoutes.VERIFY_CODE,
                                arguments: VerifyCodeParam(
                                  account: phone,
                                  platform: CodePlatform.phone,
                                  codeType: CodeType.security,
                                  securityType: controller.securityType,
                                ),
                              );
                            } else {
                              Loading.showError(local.verify_code_send_failed);
                            }
                          });
                        }),
                      ),
                    ),
                  ],
                ),
              )
            ],
          ),
        ),
      ),
    );
  }

  // 构建验证按钮
  Widget _buildVerifyButton(String title, VoidCallback onPressed) {
    return Padding(
      padding: EdgeInsets.all(CONTENT_MARGIN),
      child: LxButton(
        buttonAlignmentEnum: ButtonAlignmentEnum.block,
        height: 44.w,
        borderRadius: BorderRadius.circular(22.w),
        title: title,
        onTap: () {
          onPressed();
        },
      ),
    );
  }
}
